package com.syqc.utils

import android.annotation.SuppressLint
import com.syqc.comlib.AppConfigs
import com.syqc.comlib.UserConstant
import com.syqc.comlib.entity.CarConfig
import com.syqc.comlib.utils.AppContext
import com.syqc.comlib.utils.GsonUtil
import com.syqc.comlib.utils.toast
import com.syqc.monitor.R
import com.syqc.net.ServiceHelper
import io.reactivex.rxjava3.subjects.PublishSubject

/** APP设置工具类 */
object AppConfigUtil {
    private val _saveLive = PublishSubject.create<Boolean>()
    val saveLive = _saveLive.hide()

    //infoSwitch：通知开关；homeSet：首页设置；quickSet：快捷设置；appGpsColumn：定位信息；mapMarker：地图图标
    private val keys = arrayOf("infoSwitch", "homeSet", "quickSet", "appGpsColumn", "mapMarker")

    /**
     *  保存设置参数
     *
     * @param keyIndex
     * @param data
     * @param isShow  是否展示提示
     */
    @SuppressLint("CheckResult")
    fun saveConfig(keyIndex: Int, data: String, isShow: Boolean = true) {
        ServiceHelper.getRepository().saveAppConfig(keys[keyIndex], data).subscribe({
            _saveLive.onNext(true)
            if (isShow) AppContext.getAppContext().toast(it.result.toString())
        }, {
            _saveLive.onNext(false)
            if (it.message == AppContext.getDescById(R.string.net_error_404)) {
                return@subscribe
            }
            if (isShow) AppContext.getAppContext().toast(it.message ?: "")
        })
    }

    /** 保存单车设置参数 */
    @SuppressLint("CheckResult")
    fun saveCarConfig(carId: String, data: String, isShow: Boolean = true) {
        ServiceHelper.getRepository().saveCarCfg("[$carId]", data).subscribe({
            _saveLive.onNext(true)
            if (isShow) AppContext.getAppContext().toast(it.result.toString())
        }, {
            _saveLive.onNext(false)
            if (it.message == AppContext.getDescById(R.string.net_error_404)) {
                return@subscribe
            }
            if (isShow) AppContext.getAppContext().toast(it.message ?: "")
        })
    }

    private fun getCarConfig(): String {
        val cfg = CarConfig(
            AppConfigs.isAlarms,
            AppConfigs.isArrears,
            AppConfigs.arrearsAuth,
            AppConfigs.mapMark,
            AppConfigs.mapRefresh.toString(),
            AppConfigs.isCenter,
            AppConfigs.isOnOffLine
        )
        return GsonUtil.toJson(cfg)
    }

    /** 保存单车设置参数 */
    @SuppressLint("CheckResult")
    fun saveCarSetCfg(isShow: Boolean = true) {
        ServiceHelper.getRepository()
            .saveCarSetCfg("[${UserConstant.current_carId}]", getCarConfig()).subscribe({
                _saveLive.onNext(true)
                if (isShow) AppContext.getAppContext().toast(it.result.toString())
            }, {
                _saveLive.onNext(false)
                if (it.message == AppContext.getDescById(R.string.net_error_404)) {
                    return@subscribe
                }
                if (isShow) AppContext.getAppContext().toast(it.message ?: "")
            })
    }
}